package com.opal.modules.costReportForm.config;

import com.opal.domain.ReturnMessageData;
import com.opal.modules.costReportForm.annotation.IsInitialInfo;
import com.opal.modules.costReportForm.entity.CostReportB;
import com.opal.modules.costReportForm.entity.table.CostReportInitialInfo;
import com.opal.modules.costReportForm.mapper.mysql.CostReportBMapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.List;

@Component
@Aspect
public class ConfigInitiallnfoAop {
    @Autowired
    private CostReportBMapper costReportBMapper;

   //@Pointcut(value = "execution(* com.opal.modules.costReportForm.controller.CostReportFormController.*(..))")
    @Pointcut(value = "@annotation(com.opal.modules.costReportForm.annotation.IsInitialInfo)")
    public void is() {

    }

    @Around("is()")
    public Object doAccessCheck(ProceedingJoinPoint pjp) throws Throwable {
        //获取反射方法
        MethodSignature signature = (MethodSignature) pjp.getSignature();
        Method methods = signature.getMethod();
        IsInitialInfo methodAnnotation = methods.getAnnotation(IsInitialInfo.class);
        //判断是否有注解
        if (methodAnnotation !=null){

            Object[] arg = pjp.getArgs();
            Integer subId=-1;
            if (arg[0] instanceof Integer){
                subId= (Integer) arg[0];
            }else if (arg[0] instanceof CostReportInitialInfo){
                CostReportInitialInfo cri= (CostReportInitialInfo) arg[0];
                subId=cri.getSubId();
            }
            List<CostReportB> costReportBS = costReportBMapper.selectBySubId(subId);
            if (costReportBS ==null || costReportBS.size()<=0){
                return pjp.proceed();//放行
            }
            //查询数据库判断是否有在使用
            return new ReturnMessageData(500, "处理失败，此产品标准工时已在使用中，请重新添加操作！");
        }else{
            return pjp.proceed();//放行
        }
        
    }
}
